package resources;

import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;

import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import common.Constant;


public class Input implements InputProcessor, GestureListener{

	public static final int ESCAPE = 0;

//	public static final int ZERO = 8;
//	public static final int ONE = 9;
//	public static final int TWO = 10;
//	public static final int THREE = 11;
//	public static final int FOUR = 12;
//	public static final int FIVE = 13;
//	public static final int SIX = 14;
//	public static final int SEVEN = 14;
//	public static final int EIGHT = 15;
	
	
	public static final int PLAYGAME = 1;
	public static final int ATTACK = 2;
	public static final int ATOMIC = 14;
	public static final int HIGHSCORE = 3;
	public static final int CREDITS = 4;
	
	public static final int CLASSICMODE = 5;
	public static final int NOMALMODE = 6;
	public static final int BACK = 7;
	
	public static final int OPEN_FACEBOOK = 8;
	public static final int OPEN_TWITTER = 9;
	public static final int EXITGAME = 10;
	public static final int OPEN_CATALOG = 11;
	public static final int LOADGAME = 12;
	public static final int HELPGAME = 13;
	public static final int OPENDMARKET = 14;
	
	// KET THUC PHAN KHAI BAO CAC VUNG TRONG MARKET
	public boolean[] buttons = new boolean[64];
	public boolean[] oldButtons = new boolean[64];
	public Rectangle sellRectangle = new Rectangle(353, 287, 110,42);
	public Rectangle buyRectangle = new Rectangle(350, 143, 110,42);
	public Rectangle nextRectangle = new Rectangle(198, 93, 70,36);
	public Rectangle backRectangle = new Rectangle(198, 393, 70,36);
	public float angle = 0;
	public int xOld;
	public int xNew;
	public int yOld;
	public int yNew;
	private boolean first = true;
	private int count = 0;
	public int switchWeapon = 0;
	public Input(){
	}

	public void set (int key, boolean down) {
		int button = -1;

		if (key == Keys.H) button = ATTACK;
		if (key == Keys.I) button = ATOMIC;
		if (key == Keys.C) button = OPEN_CATALOG;

		if (key == Keys.B) button = PLAYGAME;
		if (key == Keys.E) button = HIGHSCORE;
		if (key == Keys.U) button = HELPGAME;
		if (key == Keys.F1) button = EXITGAME;
		if (key == Keys.F) button = LOADGAME;
		if (key == Keys.F7) button = CLASSICMODE;
		if (key == Keys.F8) button = NOMALMODE;
		if (key == Keys.F9) button = BACK;
		if (key == Keys.K) button = OPENDMARKET;
		
		if (key == Keys.ESCAPE || key == Keys.MENU) button = ESCAPE;

		if (button >= 0) {
			buttons[button] = down;
		}
	}

	public void tick (int screen) {
		for (int i = 0; i < buttons.length; i++) {
			oldButtons[i] = buttons[i];
		}
		if (Gdx.app.getType() == ApplicationType.Desktop) {
			if(screen == Constant.GameScreen) {
				boolean h = false;
				for (int i = 0; i < 2; i++) {
					int x = Gdx.input.getX(i);
					int y =480- Gdx.input.getY(i);
					if (!Gdx.input.isTouched(i)) continue;
					if (x >= 0 && x < 90 && y >= 0 && y <= 90)
					{
						set(Keys.H,true);
						h |= true;
					}
				}
				if (h == false) set(Keys.H, false);
			} else if(screen == Constant.MainMenuScreen){
				// truc toa do  y --tinh tu tren xuong
				//              x --tinh tu tren xuong
//				System.out.println("screen == Constant.ShopItemsScreen ===================================== "  );
//				
				for (int i = 0; i < 2; i++) {
					int x = (int)((Gdx.input.getX(i) / (float)Gdx.graphics.getWidth()) *800);
					int y = (int)((Gdx.input.getY(i) / (float)Gdx.graphics.getHeight()) * 480);
//					System.out.println("X: " + x + " - Y: " + y);
					if (!Gdx.input.isTouched(i)) continue;
					if(Constant.checkCollision(x, y, 150, 150, 265, 313)){
						System.out.println("play game ne  ============ ============ ");
						set(Keys.B, true);
					}
					else if(Constant.checkCollision(x, y, 572, 55, 720, 80)){
						System.out.println("load game ne  ============ ============ ");
						set(Keys.F, true);
					}
					else if(Constant.checkCollision(x, y, 395, 219, 511, 290)){
						System.out.println("help mode  ============ ============ ");
						set(Keys.U, true);
					}
					else if(Constant.checkCollision(x, y, 568, 258, 722, 328)){
						System.out.println("high score table  ============ ============ ");
						set(Keys.E, true);
					}
//					else if(Constant.checkCollision(x, y, 300, 328, 500, 390)){
//						set(Keys.F1, true);
//					}
					else if (x >= Constant.SCREEN_WIDTH-80 && x <= 800 && y >= 25 && y <= 100) {
						set(Keys.K, true);
					}
				}
				
			}
		}
		if (Gdx.app.getType() == ApplicationType.Android) {
			if(screen == Constant.GameScreen){
				boolean h = false;
				for (int i = 0; i < 2; i++) {
					int x = Gdx.input.getX(i);
					int y =Gdx.graphics.getHeight() - Gdx.input.getY(i);
					if (!Gdx.input.isTouched(i)) continue;
//					Gdx.app.log("MyTag", x + "," + y + "," + Gdx.graphics.getWidth() + "," + Gdx.graphics.getHeight());
					if (x >= 0 && x < 110 && y >= 0 && y <= 110)
					{
						set(Keys.H,true);
						h |= true;
					}
				}
				if (h == false) set(Keys.H, false);
				return;
			} else if(screen == Constant.MainMenuScreen){
				// truc toa do  y --tinh tu tren xuong
				//              x --tinh tu tren xuong
//				System.out.println("screen == Constant.ShopItemsScreen ===================================== "  );
//				
				for (int i = 0; i < 2; i++) {
					int x = (int)((Gdx.input.getX(i) / (float)Gdx.graphics.getWidth()) *800);
					int y = (int)((Gdx.input.getY(i) / (float)Gdx.graphics.getHeight()) * 480);
//					System.out.println("X: " + x + " - Y: " + y);
					if (!Gdx.input.isTouched(i)) continue;
					if(Constant.checkCollision(x, y, 315, 93, 470, 170)){
						System.out.println("play game ne  ============ ============ ");
						set(Keys.B, true);
					}
					else if(Constant.checkCollision(x, y, 572, 55, 720, 80)){
						System.out.println("load game ne  ============ ============ ");
						set(Keys.F, true);
					}
					else if(Constant.checkCollision(x, y, 395, 219, 511, 290)){
						System.out.println("help mode  ============ ============ ");
						set(Keys.U, true);
					}
					else if(Constant.checkCollision(x, y, 568, 258, 722, 328)){
						System.out.println("high score table  ============ ============ ");
						set(Keys.E, true);
					}
					else if(Constant.checkCollision(x, y, 300, 328, 500, 390)){
						set(Keys.F1, true);
					}
					else if (x >= Constant.SCREEN_WIDTH-80 && x <= 800 && y >= 25 && y <= 100) {
						set(Keys.K, true);
					}
				}
				
			}
			
		}
	}

	public void releaseAllKeys () {
		for (int i = 0; i < buttons.length; i++) {
			buttons[i] = false;
		}
	}

	@Override
	public boolean keyDown (int keycode) {
		set(keycode, true);
		return false;
	}

	@Override
	public boolean keyUp (int keycode) {
		set(keycode, false);
		return false;
	}

	@Override
	public boolean keyTyped (char character) {
		return false;
	}

	@Override
	public boolean touchDown (int x, int y, int pointer, int button) {
//		System.out.println("touchDown ====================================" );
//		if(screen == Constant.ChooseModeScreen){}
//		int y1 = 480-(int)((Gdx.input.getY() / (float)Gdx.graphics.getHeight()) * 480);
//		Y_DOWN = y1;
		//System.out.println("TOUCH DOWN");
		count = 0;
		return false;
	}

	@Override
	public boolean touchUp (int x, int y, int pointer, int button) {
		first = true;
		count = 0;
		return false;
	}
	public void resetMouse() {
		xOld = yOld = xNew = yNew = 0;
	}
	@Override
	public boolean touchDragged (int x, int y, int pointer) {
		float x1 = (float) Gdx.input.getX()/Gdx.graphics.getWidth()*800.0f;
		float y1 = (float) (Gdx.graphics.getHeight() - Gdx.input.getY())/Gdx.graphics.getHeight()*480.0f;
		if (x1 >= 0 && x1 < 120 && y1 >= 0 && y1 <= 120) return true;
		if (x1 >= 0 && x1 < 120 && y1 >= 280 && y1 <= 400) return true;
		if (Math.abs(x-xOld) > 0.0f || Math.abs(y-yOld) > 0.0f)
		{
			count++;
			xOld = xNew;
			yOld = yNew;
			xNew = x;
			yNew = y;
		}
		return true;
	}
	public float getAngle() {
		return angle;
	}
	public boolean getFirst() {
		return first;
	}
	public int getCount() {
		return count;
	}

	@Override
	public boolean scrolled(int arg0) {
		Gdx.app.log("MyTag", "Test");
		return false;
	}

	@Override
	public boolean pinch(Vector2 arg0, Vector2 arg1, Vector2 arg2, Vector2 arg3) {
		Gdx.app.log("MyTag", "Test");
		return false;
	}

	@Override
	public boolean zoom(float arg0, float arg1) {
		Gdx.app.log("MyTag", "Test");
		return false;
	}

	@Override
	public boolean fling(float arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean longPress(float arg0, float arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean pan(float arg0, float arg1, float arg2, float arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean tap(float arg0, float arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(float arg0, float arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean mouseMoved(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}
}
